package com.runlooker.runer.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.runlooker.runer.application.MyApplication;
import com.runlooker.runer.db.RunerDBOpenHelper;
import com.runlooker.runer.db.RunerTableSql;
import com.runlooker.runer.entity.LocalRosterEntity;
import com.runlooker.runer.util.L;
import com.runlooker.runer.util.PreferenceUtils;

public class RosterEntryDao {
	private final String tag = RosterEntryDao.class.getSimpleName();
	private String mTable = RunerTableSql.ROSTER_ENTRY_TABLE;
	private RunerDBOpenHelper mRunerDBOpenHelper;
	private Context context;

	public RosterEntryDao(Context context) {
		mRunerDBOpenHelper = new RunerDBOpenHelper(context);
		this.context = context;
	}

	/**
	 * 
	 * 获取我订阅的所有人(itemType是both或to数据)
	 * 
	 * @return list
	 */
	public synchronized List<LocalRosterEntity> getMySubscription() {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		List<LocalRosterEntity> list = new ArrayList<LocalRosterEntity>();
		LocalRosterEntity entry = null;
		Cursor cursor = db.query(mTable, null, null, null, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				entry = new LocalRosterEntity();
				entry.setAccount(cursor.getString(cursor
						.getColumnIndex("account")));
				entry.setGroupsName(cursor.getString(cursor
						.getColumnIndex("groupsName")));
				entry.setItemType(cursor.getInt(cursor
						.getColumnIndex("itemType")));
				entry.setName(cursor.getString(cursor.getColumnIndex("name")));
				entry.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
				if (entry.getItemType() == LocalRosterEntity.TO
						|| entry.getItemType() == LocalRosterEntity.BOTH) {
					list.add(entry);
				}
			}
		}

		cursor.close();
		// db.close();
		return list;
	}

	public synchronized List<LocalRosterEntity> getRosterEntities() {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		List<LocalRosterEntity> list = new ArrayList<LocalRosterEntity>();
		LocalRosterEntity entry = null;
		Cursor cursor = db.query(mTable, null, null, null, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				entry = new LocalRosterEntity();
				entry.setAccount(cursor.getString(cursor
						.getColumnIndex("account")));
				entry.setGroupsName(cursor.getString(cursor
						.getColumnIndex("groupsName")));
				entry.setItemType(cursor.getInt(cursor
						.getColumnIndex("itemType")));
				entry.setName(cursor.getString(cursor.getColumnIndex("name")));
				entry.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
				list.add(entry);
			}
		}

		cursor.close();
		// db.close();
		return list;
	}

	/**
	 * 
	 * 获得所有联系人
	 * 
	 * @return list
	 */
	public synchronized LocalRosterEntity getRoster(String account) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		LocalRosterEntity entry = null;
		String sql = "SELECT * FROM " + mTable + " WHERE account = " + "'"
				+ account + "'";
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor != null && cursor.moveToNext()) {
			entry = new LocalRosterEntity();
			entry.setAccount(cursor.getString(cursor.getColumnIndex("account")));
			entry.setGroupsName(cursor.getString(cursor
					.getColumnIndex("groupsName")));
			entry.setItemType(cursor.getInt(cursor.getColumnIndex("itemType")));
			entry.setName(cursor.getString(cursor.getColumnIndex("name")));
			entry.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
		}
		cursor.close();
		// db.close();
		return entry;
	}

	/**
	 * 
	 * 清空表
	 * 
	 * @Title: clearTable
	 * @Description: 方法描述
	 */
	public synchronized void clearTable() {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		StringBuffer updateSql = new StringBuffer("DELETE FROM " + mTable);
		db.execSQL(updateSql.toString());
		// db.close();
	}

	/**
	 * 保存单个联系人的信息
	 * 
	 * @param entry
	 * @param db
	 */
	private synchronized void saveRosterEntry(LocalRosterEntity entry,
			SQLiteDatabase db) {
		// SQLiteDatabase db = mDBService.getReadableDatabase();
		if (entry == null) {
			// db.close();
			return;
		}

		ContentValues values = new ContentValues();
		values.put("account", entry.getAccount());
		values.put("name", entry.getName());
		values.put("status", entry.getStatus());
		values.put("itemType", entry.getItemType());
		values.put("groupsName", entry.getGroupsName());
		long rowId = db.insert(mTable, null, values);
		// db.close();
	}

	public synchronized void saveOrUpdateRosterEntry(LocalRosterEntity entry) {
		if (entry == null) {
			return;
		}
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		if (IsExists(entry.getAccount(), db)) {
			updateRosterEntry(entry, db);
		} else {
			saveRosterEntry(entry, db);
		}
		// db.close();
	}

	/**
	 * 更新联系人信息
	 * 
	 * @param entry
	 * @param db
	 */
	private synchronized void updateRosterEntry(LocalRosterEntity entry,
			SQLiteDatabase db) {
		if (entry == null) {
			return;
		}

		ContentValues values = new ContentValues();
		values.put("account", entry.getAccount());
		values.put("name", entry.getName());
		values.put("status", entry.getStatus());
		values.put("itemType", entry.getItemType());
		values.put("groupsName", entry.getGroupsName());
		String[] args = { entry.getAccount() };
		long rowId = db.update(mTable, values, "account = ?", args);
		// db.close();
	}

	/**
	 * 
	 * 根据帐号删除联系人
	 * 
	 * @param account
	 */
	public synchronized void deleInfoByAccount(String account) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		String sqlString = "DELETE FROM " + mTable + " WHERE account = " + "'"
				+ account + "'";
		db.execSQL(sqlString);
	}

	/**
	 * 
	 * 判断联系人是否存在数据库中
	 * 
	 * @param entry
	 * @return
	 */
	private boolean IsExists(String account, SQLiteDatabase db) {
		boolean flag = false;
		Cursor cursor = db.query(mTable, null, "account = ?",
				new String[] { account }, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			flag = true;
		} else {
			flag = false;
		}
		cursor.close();
		return flag;
	}

}
